<?php
namespace app\services;

use app\models\Log;

class OperateLogService extends BaseService {
    public $matrix = [
        'id'            => 'id',
        'userId'        => 'user_name',
        'userType'      => ['user_type','$v=="1" && $v="root";
                                         $v=="2" && $v=\Yii::t("log", "Operator");
                                         $v=="3" && $v=\Yii::t("log", "Agent");
                                         $v=="4" && $v=\Yii::t("log", "Advertiser");
                                         return $v; '],
        'ip'            => 'request_ip',
        'operateObject' => 'model',
        'operateDate'   => 'last_update',
        'operate'       => 'action',
    ];

    public function getLogs() {
        $user = $this->M('User', $this->S('user_id'));
        $us = new UsersService();
        // $userRole = $us->getUserType($user);
        switch ($user['user_type']) {
            case 1:
                $time = date('Y-m-d 00:00:00');
                $where  = "last_update>='$time'";
                break;
            case 2:
            case 3:
            case 4:
                $subordinates = $us->getUserSubordinateIds($user, 1);
                $subordinates[] = $user['id'];
                foreach ($subordinates as $key => $value) {
                    $subordinates[$key] = "'$value'";
                }
                $where1 = implode(' , ', $subordinates);
                $where1 = "user_id in (".$where1.")";
                $where[] = $where1;

                $time = date('Y-m-d 00:00:00');
                $where2 = "last_update>='$time'";
                $where[] = $where2;
                $where = implode(' and ', $where);
                break;
            default:
                break;
        }
        $sql = "select id, user_name, user_type, request_ip, model, last_update 
                from {{%log}}
                where $where
                limit 300";
        $logs = $this->query($sql);
        $logs = array_reverse($logs);
        $logs = $this->fieldsMap($this->matrix, $logs);
        return $logs;
    }
}// end

